import 'package:flutter/material.dart';

/// @date 2020/08/06  10:04
/// @author 程志强
/// @des 原始指针事件处理

class EventListenerRoute extends StatefulWidget {
  @override
  _EventListenerRouteState createState() => _EventListenerRouteState();
}

class _EventListenerRouteState extends State<EventListenerRoute> {
  // 定义一个状态，保存当前指针位置
  PointerEvent _event;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("原始指针事件处理"),
      ),
      body: Column(
        children: <Widget>[
          Listener(
            child: Container(
              alignment: Alignment.center,
              color: Colors.blue,
              width: double.infinity,
              height: 200.0,
              child: Text(
                  "偏移位置position:${_event?.position}\n两次指针delta：${_event?.delta}\n按压力度pressure："
                  "${_event?.pressure}\n指针移动方向orientation：${_event?.orientation}",
                  style: TextStyle(color: Colors.white)),
            ),
            onPointerDown: (PointerEvent e) => setState(() => _event = e),
            onPointerMove: (PointerEvent e) => setState(() => _event = e),
            onPointerUp: (PointerEvent e) => setState(() => _event = e),
          ),
          Text("behavior决定子组件如何响应命中测试"),
          Stack(
            children: <Widget>[
              Listener(
                child: ConstrainedBox(
                  constraints: BoxConstraints.tight(Size(300.0, 200.0)),
                  child: DecoratedBox(
                      decoration: BoxDecoration(color: Colors.blue)),
                ),
                onPointerDown: (event) => print("down0"),
              ),
              Listener(
                child: ConstrainedBox(
                  constraints: BoxConstraints.tight(Size(200.0, 100.0)),
                  child: Center(child: Text("左上角200*100范围内非文本区域点击")),
                ),
                onPointerDown: (event) => print("down1"),
                behavior: HitTestBehavior.translucent, //放开此行注释后可以"点透"
              )
            ],
          ),
          Text("使用AbsorbPointer忽略PointerEvent"),
          Listener(
            child: AbsorbPointer(
              child: Listener(
                child: Container(
                  color: Colors.red,
                  width: 200.0,
                  height: 100.0,
                ),
                onPointerDown: (event) => print("in"),
              ),
            ),
            onPointerDown: (event) => print("up"),
          )
        ],
      ),
    );
  }
}
